package com.longxi.tianjintianbao.ui.presenter.patrol;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.patrol.Patrol;
import com.longxi.tianjintianbao.ui.activity.patrol.PatrolDetailActivity2;
import com.longxi.tianjintianbao.ui.adapter.multitype.patrol.PatrolListViewBinder2;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.patrol.IPatrolListAtView2;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PatrolListAtPresenter2 extends BasePresenter<IPatrolListAtView2> {

    private MultiTypeAdapter adapter;
    private Items items = new Items();

    public PatrolListAtPresenter2(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("巡检");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
        initAdapter();
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Patrol.class, new PatrolListViewBinder2(new PatrolListViewBinder2.OnSelectPatrolListener() {
                @Override
                public void onSuccess(Patrol patrol) {
                    Intent mainIntent = new Intent(mContext, PatrolDetailActivity2.class);
                    mainIntent.putExtra(AppConst.BILLID, patrol.getBillid());
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_PATROL_DETAIL);
                }
            }));
            getView().getRvContent().setAdapter(adapter);
            adapter.setItems(items);
        }
    }

    public void getInspectionList(){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getInspectionList2(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Patrol>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Patrol> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    @Override
    public void detachView() {
        super.detachView();
        this.adapter = null;
        this.items = null;
    }
}
